package com.kcht.rfid.rd30.secretary.dispatch;

import com.kcht.rfid.rd30.secretary.core.command.ReaderCommand;

import java.util.Observer;

/**
 * 单个rd30阅读器的接口,模板模式，可支持多种方式的检测，多种信道
 * */
public  class Rd30Reader{
    /**
     * 连接端口
     * */
    private int port=10001;

    /***
     * 异常检测
     * */
    private BaseAbnormalDetection detection;

    /**
     * 信道
     * */
    private IReaderChannel channel;

    /**
     * 命令执行
     * */
    private BaseCommandExecute commandExecute;


    public Rd30Reader(IReaderChannel channel, BaseAbnormalDetection detection, BaseCommandExecute commandExecute){
        this.channel=channel;
        this.detection=detection;
        this.commandExecute=commandExecute;
        detection.detection(this.channel);
    }

    /**
     * 关闭检测
     * 断开连接
     * */
    public synchronized void disconnect(){
        commandExecute.interrupt();
        detection.closeDetection();
        channel.close();
    }

    /**
     * 执行命令
     * */
    public synchronized void executeCommand(ReaderCommand cmd, Observer[] observers){
        commandExecute.execute(channel,cmd,observers);
    }

    /**
     * 执行完一个不可分割单元后，停止执行
     * */
    public synchronized void stopWork(){
        commandExecute.interrupt();
    }
}
